#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: ts=4 sw=4 et ai ft=python

import sys

try:
    from Tkinter import *
except ImportError:
    print "You need to install python-tkinter dependency"
    sys.exit(-1)

try:
    import BeautifulSoup
except ImportError:
    print "You need to install python-beautifulSoup library"
    sys.exit(-1)

import urllib2

import ng
import flickrinterest
import ybuzz
import subprocess


def main():
    root = Tk()


    def run(event):
        proxy = {}
        if x.get():
            proxy['IP'] = vip.get()
            proxy['USER'] = vuser.get()
            proxy['PASSWORD'] = vpass.get()
            proxy['PORT'] = '80'

        if vradio.get() == 1:
            ng.setdesktopwallpaper.run(proxy)
        elif vradio.get() == 2:
            flickrinterest.setdesktopwallpaper.run(proxy)
        elif vradio.get() == 3:
            ybuzz.setdesktopwallpaper.run(proxy)
        else:
            print 'Please select a value'
        root.destroy()

    def schedule(event):
        proxy = {}
        if x.get():
            proxy['IP'] = vip.get()
            proxy['USER'] = vuser.get()
            proxy['PASSWORD'] = vpass.get()
            proxy['PORT'] = '80'

        if vradio.get() == 1:
            ng.setdesktopwallpaper.run(proxy)
            ng.setschedule.run(proxy)
        elif vradio.get() == 2:
            flickrinterest.setdesktopwallpaper.run(proxy)
            flickrinterest.setschedule.run(proxy)
        elif vradio.get() == 3:
            ybuzz.setdesktopwallpaper.run(proxy)
            ybuzz.setschedule.run(proxy)
        else:
            print 'Please select a value.'
        root.destroy()

    root.title('Desktop Wallpaper Love')
    Label(root, text='Desktop Wallpaper Love', relief=FLAT, justify=
          CENTER).grid(row=0, column=0, columnspan=2)

    vradio = IntVar()
    Radiobutton(root, text='National Geographic', variable=vradio, value=
                1, relief=FLAT, justify=CENTER).grid(row=2, column=0, 
            columnspan=2)
    Radiobutton(root, text='Flickr Interesting ', variable=vradio, value=
                2, relief=FLAT, justify=CENTER).grid(row=3, column=0, 
            columnspan=2)
    Radiobutton(root, text='Yahoo Buzz         ', variable=vradio, value=
                3, relief=FLAT, justify=CENTER).grid(row=4, column=0, 
            columnspan=2)

    runnow = Button(root, text='Run')
    runnow.grid(row=5, column=0, sticky=W)
    runnow.bind('<Button-1>', run)
    scheduleit = Button(root, text='Schedule')
    scheduleit.grid(row=5, column=1, sticky=E)
    scheduleit.bind('<Button-1>', schedule)
    ip = Label(root, text='Proxy IP:')
    vip = Entry(root, width=20)
    user = Label(root, text='User:')
    vuser = Entry(root, width=20)
    pwd = Label(root, text='Password:')
    vpass = Entry(root, width=20, show='*')

    def display():
        if x.get():
            ip.grid(row=7, column=0)
            vip.grid(row=7, column=1)
            user.grid(row=8, column=0)
            vuser.grid(row=8, column=1)
            pwd.grid(row=9, column=0)
            vpass.grid(row=9, column=1)
        else:
            ip.grid_forget()
            vip.grid_forget()
            user.grid_forget()
            vuser.grid_forget()
            pwd.grid_forget()
            vpass.grid_forget()

    x = StringVar()
    proxy = Checkbutton(root, text='Enable Proxy', variable=x, onvalue=
                        'proxy', offvalue='', command=display)
    proxy.grid(row=6, column=0)
    root.wait_window()


if __name__ == '__main__':
    main()
